import { MOVE_SPEED } from "./Constant";

const { regClass, property } = Laya;

@regClass()
export class Monster extends Laya.Script {
    //declare owner : Laya.Sprite3D;
    declare owner: Laya.Sprite;

    @property(Laya.Animation)
    public aniMonster: Laya.Animation;

    private gridX_start: number = 0;
    private gridY_start: number = 0;

    private tiled_width: number = 0;
    private tiled_height: number = 0;

    private runPaths: any[] = [];

    private call_back: Laya.Handler = null;

    private end_attack = true;

    private onMonsterAttackFinish() {
        this.call_back && this.call_back.run();
    }

    setStarGrid(x: number, y: number, tw: number, th: number) {
        this.gridX_start = x;
        this.gridY_start = y;
        this.tiled_width = tw;
        this.tiled_height = th;
        this.owner.pos(x * tw + tw / 2, y * th + th / 2);
    }

    get StartGridX(): number {
        return this.gridX_start;
    }

    get StartGridY(): number {
        return this.gridY_start;
    }

    playRun(path: any[], cb?: Laya.Handler, attack = true) {
        this.aniMonster.loadAtlas("resources/frames/boss_walk_left.atlas");
        this.aniMonster.scaleX = 1;
        this.aniMonster.play(0, true);
        this.runPaths = path;
        this.call_back = cb;
        this.end_attack = attack;
    }

    onMonsterAttack() {
        this.aniMonster.loadAtlas("resources/frames/boss_attack_left.atlas");
        this.aniMonster.off(Laya.Event.COMPLETE, this, this.onMonsterAttackFinish);
        this.aniMonster.on(Laya.Event.COMPLETE, this, this.onMonsterAttackFinish);
        this.aniMonster.play(0, false);
        Laya.SoundManager.playSound("resources/sound/attack.mp3");
    }

    //每帧更新时执行，尽量不要在这里写大循环逻辑或者使用getComponent方法
    onUpdate(): void {
        if (this.runPaths.length > 0) {
            let path = this.runPaths[0];
            let x = path.x * this.tiled_width + this.tiled_width / 2;
            let y = path.y * this.tiled_height + this.tiled_height / 2;
            if (x > this.owner.x) {
                this.aniMonster.scaleX = -1;
            } else {
                this.aniMonster.scaleX = 1;
            }
            if (Math.abs(x - this.owner.x) < MOVE_SPEED && Math.abs(y - this.owner.y) < MOVE_SPEED) {
                this.owner.pos(x, y);
                this.runPaths.shift();

                if (this.end_attack && this.runPaths.length == 0) {
                    this.onMonsterAttack();
                }
            } else {
                if (Math.abs(x - this.owner.x) >= MOVE_SPEED) {
                    if (x > this.owner.x) {
                        this.owner.x += MOVE_SPEED;
                    } else {
                        this.owner.x -= MOVE_SPEED;
                    }
                } else if (Math.abs(y - this.owner.y) >= MOVE_SPEED) {
                    if (y > this.owner.y) {
                        this.owner.y += MOVE_SPEED;
                    } else {
                        this.owner.y -= MOVE_SPEED;
                    }
                }
            }
        }
    }

    onDisable(): void {
        this.aniMonster.offAllCaller(this);
    }
}